package com.itheima.health.controller.user;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.VerificationService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {


    @Autowired
    private VerificationService verificationService;

    @Autowired
    public MemberService memberService;

    @Autowired
    public OrderService orderService;

    @Autowired
    private SetMealService setMealService;

    @PostMapping("/submit")
    @Transactional
    public Result submit(@RequestBody OrderSubmitParam param) {


        Result order1 = verificationService.verifyCode("ORDER", param.getTelephone(), param.getValidateCode());
        if(!order1.getFlag()){
            return order1;
        }
        //创建会员 memberService
        Member member = new Member();
        member.setIdCard(param.getIdCard());
        member.setPhoneNumber(param.getTelephone());
        member.setName(param.getName());
        member.setSex(param.getSex());
        System.out.println("param = " + param.toString());
        member = memberService.create(member);

        ////提交体检信息
        Order order = new Order();
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(param.getSetMealId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderDate(param.getOrderDate());
        order.setMemberId(member.getId());
        order =  orderService.addOrder(order);


        //返回结果
        return new Result(true, MessageConst.ORDER_SUCCESS,order);
        //return new Result(true, MessageConst.ORDER_SUCCESS);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("查询订单详情");
        return orderService.selectById(id);
    }

}

